

db.define_table('plugin',
    Field('name', 'string', label=T('Name'), unique=True,
          requires=[IS_NOT_EMPTY(error_message=T('You must specify a unique name')),
                    IS_NOT_IN_DB(db, 'plugin.name', 
                                 error_message=T('Plugin already exists by this name'))],
    ),
    Field('short_description', 'string', length=255,
          label=T('Short Description'),
          comment=T('A short description of the plugin'),
    ),
    Field('description', 'text', label=T('Description'), 
          requires=IS_NOT_EMPTY(error_message=T('Coffee anyone?')),
          comment=T('Include any information about the plugin, event changelog information.')
    ),
    Field('category', 'string', label=T('Category'),
          comment=T('What type of plugin is it?'),
          requires=IS_NOT_EMPTY(error_message=T('Just to organizing things!'))
    ),
    Field('license', 'string', label=T('License'),
          requires = IS_IN_SET((
            'GNU General Public License (GPL) v2',
            'GNU Lesser General Public License (LGPL) v2',
            'GNU General Public License (GPL) v3',
            'GNU Lesser General Public License (LGPL) v3',
            'MIT License',
            'BSD License',
            'Apache Software License 2.0',
            'Eclipse Public License',
            'Mozilla Public License (MPL) 1.1',
            'Common Development and Distribution License',
            'Common Public License 1.0',
        ), error_message='You must release the code under an OSI approved license'),
        comment=T('Open source please!'),
    ),
    Field('version', 'string', label=T('Version'),
          comment=T('Ex: 1.2.1'), default='1.0.0'),
    Field('file', 'upload', 
          requires=IS_UPLOAD_FILENAME(filename='^web2py.plugin.*',
                                      extension="^w2p$",
                                      lastdot=True,
                                      case=1,
                                      error_message=T('Invalid file type')),
          label=T('Plugin pack file'), 
          comment=T('ex: web2py.plugin.comments.w2p')
    ),
    Field('screenshot', 'upload',
          label=T('Screenshot'),
          comment=T('A picture of the plugin in action. Resized to `150x120 px` (optional)')
    ),
    Field('demo_app', 'upload',
          requires=IS_NULL_OR(IS_UPLOAD_FILENAME(filename='^web2py.app.*',
                                      extension="^w2p$",
                                      lastdot=True,
                                      case=1,
                                      error_message=T('Invalid file type'))),
          label=T('Demo Application'),
          comment=T('ex: `web2py.app.blog.w2p` web2py app plugin demonstration (requires approval before installation) (optional)')
    ),    
    Field('demo_approved', 'boolean', default=False,
          readable=False, writable=False),
    
    Field('created_on', 'datetime', default=request.now,
          label=T('Created On'), writable=False, readable=False),
    Field('modified_on', 'datetime', default=request.now,
          update=request.now, label=T('Modified On'), writable=False,
          readable=False),
    Field('created_by', db.auth_user, default=auth.user.id if auth.user else 0,
          label=T('Created By'), writable=False, readable=False),
    Field('modified_by', db.auth_user, default=auth.user.id if auth.user else 0,
          update=auth.user.id if auth.user else 0, label=T('Modified By'), writable=False,
          readable=False),
    format='%(author)s',
)

class Plugin():
    @staticmethod
    def get_demo_url(plugin):
        if plugin.demo_approved:
            return URL(a='plugin_central_' + plugin.name,
                       c='default',
                       f='index')
        else:
            return "#"
        
    @staticmethod
    def install(plugin):
        from gluon.admin import *
        from gluon.fileutils import cleanpath
        import re        
        items = re.compile('(?P<table>.*?)\.(?P<field>.*?)\..*')\
                           .match(plugin.demo_app)
        (t, f) = (items.group('table'), items.group('field'))
        field = db[t][f]
        (filename, stream) = field.retrieve(plugin.demo_app)
        
        appname = 'plugin_central_' + cleanpath(plugin.name).replace('.', '_')
        
        app_install(appname, stream, request, filename, overwrite=True)
        
        return True
    
db.define_table('featured',
    Field('id_plugin', db.plugin)
)